package com.rede.didiok.search.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.common.annotion.FeignSecurity.FeignSecurity;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.dto.es.RankMemberEsModel;
import com.rede.didiok.common.enums.BizCodeEnum;
import com.rede.didiok.common.utils.JsonUtils;
import com.rede.didiok.common.utils.R;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.search.service.ESRankMemberService;
import com.rede.didiok.search.vo.SearchParamVo;
import com.rede.didiok.search.vo.SearchResultVo;

@RequestMapping("/search/feign/aggEsDoc")
@Api(value = "ElasticSearch相关接口", tags = {"ElasticSearch相关接口"})
@RestController
@Slf4j
public class ESRankMemberController {


    @Autowired
    ESRankMemberService eSRankMemberService;

    @GetMapping("/hello")
    public String hellEs() {
        return "hello es";
    }

    @FeignSecurity
    @ApiOperation(value = "批量删除Es索引", notes = "批量删除Es索引", response = String.class)
    @PostMapping("/batchDeleteEsIndex")
    public String batchDeleteEsIndex(@RequestBody List<String> aggEsDocIdList) throws IOException {
        log.info("开始批量删除Es索引:" + JsonUtils.objectToJson(aggEsDocIdList));
        if (aggEsDocIdList.size() > 0) {
            eSRankMemberService.bulkDelete(aggEsDocIdList);
        }
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.DELETE_SUCCESS);
    }

    @FeignSecurity
    @ApiOperation(value = "批量更新Es索引", notes = "ElasticSearch初始化索引", response = String.class)
    @PostMapping("/batchUpdateEsIndex")
    public String batchUpdateEsIndex(@RequestBody List<RankMemberEsModel> memberEsModels) throws IOException {
        log.info("开始批量更新Es索引:" + JsonUtils.objectToJson(memberEsModels));
        // 批量更新
        boolean success = eSRankMemberService.bulkUpdate(memberEsModels);
        if (!success) {
            return ResultUtil.result(SysConf.ERROR, MessageConf.OPERATION_FAIL);
        }
        return ResultUtil.result(SysConf.SUCCESS, MessageConf.OPERATION_SUCCESS);
    }

    /**
     * 发布榜单成员
     * @param memberEsModels
     * @return
     */
    @FeignSecurity
    @ApiOperation(value = "批量创建Es索引", notes = "ElasticSearch初始化索引", response = String.class)
    @PostMapping("/rank")
    public R rankRankMemberPublish(@RequestBody List<RankMemberEsModel> memberEsModels) {
        boolean b = false;
        try {
            b = eSRankMemberService.bulkCreate(memberEsModels);
        } catch (Exception e) {
            log.error("ElasticSaveController榜单成员发布错误：{}", e);
            return R.error(BizCodeEnum.RANK_MEMBER_PUBLISH_EXCEPTION.getCode(), BizCodeEnum.RANK_MEMBER_PUBLISH_EXCEPTION.getMsg());
        }

        if (!b) {
            return R.ok();
        } else {
            return R.error(BizCodeEnum.RANK_MEMBER_PUBLISH_EXCEPTION.getCode(), BizCodeEnum.RANK_MEMBER_PUBLISH_EXCEPTION.getMsg());
        }
    }

    /**
     * 自动将页面提交过来的所有请求查询参数封装成指定的对象
     * localhost:12000/list.html?catalog3Id=225&keyword=程序员&sort=voteCount_asc&rankId=1&rankId=2&gradeId=8&gradeId=9&attrs=1_人工智能:嵌入式&attrs=2_陕西科技大学:中国计量大学
     * @param param
     * @return
     */
    @ApiOperation(value = "通过ElasticSearch搜索聚合内容", notes = "通过ElasticSearch搜索博客", response = String.class)
    @GetMapping("/rankMember/search")
    public String search(SearchParamVo param, HttpServletRequest request){
        log.info("通过ES聚合搜索内容：" + JsonUtils.objectToJson(param));

        param.set_queryString(request.getQueryString());
        //1、根据传递来的页面的查询参数，去es中检索商品
        SearchResultVo result = eSRankMemberService.search(param);

        return ResultUtil.successWithData(result);

    }
}
